package cx3k.scheduler.objects;

import java.util.ArrayList;
import java.util.Collection;
import cx3k.GUI.ScheduledCourseGroup;
import cx3k.scheduler.wrappers.CourseWrapper;

public class ScheduledSemester{ 
	
	public static final int NO_SEMESTER_ID = -1;
	public static final int NO_YEAR = 0;
	public static final String NO_TERM = "No term";
	
	private int semesterId;
	private int year;
	private String term;
	
	private ArrayList<ScheduledCourseGroup> semClasses;
	
	public ScheduledSemester(){
		this(NO_SEMESTER_ID, NO_YEAR, NO_TERM);
	}
	
/*	public ScheduledSemester(ScheduledSemester copy){
		this(copy.getSemesterId(), copy.getYear(), copy.getTerm());
		
		for(CourseWrapper course : copy.getCourses()){
			this.addCourse(CopyUtil.deepCopyCourseWrapper(course));
		}
	}*/
	
	//public ScheduledSemester(int semesterId){
	//	this(semesterId, NO_YEAR, NO_TERM);
	//}
	
	public ScheduledSemester(int year, String term){
		this(NO_SEMESTER_ID, year, term);
	}
	
	public ScheduledSemester(int semesterId, int year, String term){
		setSemesterId(semesterId);
		setYear(year);
		setTerm(term);
		semClasses = new ArrayList<ScheduledCourseGroup>();
	}
	
	public boolean addCourse(ScheduledCourseGroup newSchedCrsGrp){
		//ScheduledCourseGroup scg = new ScheduledCourseGroup(newSchedCrsGrp);
		semClasses.add(newSchedCrsGrp);
		return true;
	}
	
	public boolean addCourse(Course newSchedCrsGrp){
		ScheduledCourseGroup scg = new ScheduledCourseGroup(newSchedCrsGrp);
		semClasses.add(scg);
		return true;
	}
	
	public boolean addCourses(Collection<CourseWrapper> courses){
		return getCourses().addAll(courses);
	}
	
	public ArrayList<CourseWrapper> getCourses(){
		ArrayList<ScheduledCourseGroup> temp = new ArrayList<ScheduledCourseGroup>();
		
		for(int i = 0; i < semClasses.size(); i++){
			temp.add(semClasses.get(i));
		}
		
		ArrayList<CourseWrapper> temp2 = new ArrayList<CourseWrapper>();
		for(int i = 0; i < temp.size(); i++){
			temp2.add(new CourseWrapper(temp.get(i).getCourses()[0].getCourseId()));  // TODO: fix 
		}
		
		
		return temp2;
	}
	
	public int getSemesterId(){
		return semesterId;
	}
	
	public String getTerm(){
		return term;
	}
	
	public int getYear(){
		return year;
	}
	
	public boolean removeCourse(ScheduledCourseGroup courseGroup){
		return semClasses.remove(courseGroup);
	}
	
	public void setCourses(ArrayList<ScheduledCourseGroup> courses){
		this.semClasses = courses;
	}
	
	public void setSemesterId(int semesterId){
		this.semesterId = semesterId;
	}
	
	public void setTerm(String term){
		this.term = term;
	}
	
	public void setYear(int year){
		this.year = year;
	}
	
	@Override
	public boolean equals(Object obj){
		try{
			ScheduledSemester other = (ScheduledSemester) obj;
			
			return this.getSemesterId() == other.getSemesterId() &&
				   this.getYear() == other.getYear() &&
				   this.getTerm() == other.getTerm();
			
		} catch(ClassCastException e){
			return false;
		}
	}

	public ScheduledCourseGroup[] getCourseGroups() {
		ScheduledCourseGroup c[] = new ScheduledCourseGroup[this.semClasses.size()];
		
		int i = 0;
		for (ScheduledCourseGroup scg: this.semClasses) {
			c[i++] = scg;
		}
		return c;
	}
	
	public ArrayList<ScheduledCourseGroup> getGroups() {
		return this.semClasses;
	}
	
	
	
	
	
	
}
